home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 September / PCWorld_2008-09_cd.bin / v cisle / sadanastroju / cacheviewer-0.4.7.1-fx.xpi / chrome / cacheviewer.jar / content / cacheviewer / rdf.js < prev    next >
Text File  |  2008-03-03  |  7KB  |  214 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents (ry
  5.  *
  6.  * inspired by bbs2chreader
  7.  *
  8.  * ***** END LICENSE BLOCK *****
  9.  */
  10.  
  11. function RDF() {
  12.     this._ds = Cc["@mozilla.org/rdf/datasource;1?name=in-memory-datasource"]
  13.                 .createInstance(Ci.nsIRDFDataSource);
  14. }
  15.  
  16. RDF.prototype = {
  17.     
  18.     get NS_CACHEVIEWER() {
  19.         return "http://park2.wakwak.com/~benki/cacheviewer#";
  20.     },
  21.     
  22.     get RDF_ITEM_ROOT() {
  23.         return "urn:cache:item:root";
  24.     },
  25.     
  26.     get RDF_ITEM_SEARCH() {
  27.         return "urn:cache:item:search";
  28.     },
  29.     
  30.     get datasource() {
  31.         return this._ds;
  32.     },
  33.     
  34.     get rdfService() {
  35.         if ( !this._rdfService ) {
  36.             this._rdfService = Cc['@mozilla.org/rdf/rdf-service;1']
  37.                                     .getService(Ci.nsIRDFService);
  38.         }
  39.         return this._rdfService;
  40.     },
  41.     _rdfService: null,
  42.     
  43.     get rdfContainerUtils() {
  44.         if ( !this._rdfContainerUtils ) {
  45.             this._rdfContainerUtils = Cc['@mozilla.org/rdf/container-utils;1']
  46.                                     .getService(Ci.nsIRDFContainerUtils);
  47.         }
  48.         return this._rdfContainerUtils;
  49.     },
  50.     _rdfContainerUtils: null,
  51.     
  52.     getResource: function(aResourceID) {
  53.         return this.rdfService.GetResource(aResourceID);
  54.     },
  55.     
  56.     getLiteral: function(aValue) {
  57.         return this.rdfService.GetLiteral(aValue);
  58.     },
  59.     
  60.     getIntLiteral: function(aValue) {
  61.         return this.rdfService.GetIntLiteral(aValue);
  62.     },
  63.     
  64.     getDateLiteral: function(aValue) {
  65.         return this.rdfService.GetDateLiteral(aValue);
  66.     },
  67.     
  68.     setLiteralProperty: function(aResourceOrID, aProperty, aValue) {
  69.         var source = (typeof(aResourceOrID)=="string") ?
  70.                 this.getResource(aResourceOrID) : aResourceOrID;
  71.         var property = this.getResource(aProperty);
  72.         var value = this.getLiteral(aValue);
  73.         
  74.         this._ds.Assert(source, property, value, true);
  75.     },
  76.     
  77.     getLiteralProperty: function(aResourceOrID, aProperty) {
  78.         var source = (typeof(aResourceOrID)=="string") ?
  79.                         this.getResource(aResourceOrID) : aResourceOrID;
  80.         var target = this._ds.GetTarget(source, this.getResource(aProperty), true);
  81.         if (target) return target.QueryInterface(Ci.nsIRDFLiteral).Value;
  82.         return null;
  83.     },
  84.     
  85.     setIntProperty: function(aResourceOrID, aProperty, aValue) {
  86.         var source = (typeof(aResourceOrID)=="string") ?
  87.                         this.getResource(aResourceOrID) : aResourceOrID;
  88.         var property = this.getResource(aProperty);
  89.         var value = this.getIntLiteral(aValue);
  90.         
  91.         this._ds.Assert(source, property, value, true);
  92.     },
  93.     
  94.     getIntProperty: function(aResourceOrID, aProperty) {
  95.         var source = (typeof(aResourceOrID)=="string") ?
  96.                             this.getResource(aResourceOrID) : aResourceOrID;
  97.         var target = this._ds.GetTarget(source, this.getResource(aProperty), true);
  98.         if (target) return target.QueryInterface(Ci.nsIRDFInt).Value;
  99.         return null;
  100.     },
  101.     
  102.     setDateProperty: function(aResourceOrID, aProperty, aValue) {
  103.         var source = (typeof(aResourceOrID)=="string") ?
  104.                             this.GetResource(aResourceOrID) : aResourceOrID;
  105.         var property = this.getResource(aProperty);
  106.         var value = this.getDateLiteral(aValue);
  107.         
  108.         this._ds.Assert(source, property, value, true);
  109.     },
  110.     
  111.     getDateProperty: function(aResourceOrID, aProperty) {
  112.         var source = (typeof(aResourceOrID)=="string") ?
  113.                             this.getResource(aResourceOrID) : aResourceOrID;
  114.         var target = this._ds.GetTarget(source, this.getResource(aProperty), true);
  115.         if (target) return target.QueryInterface(Ci.nsIRDFDate).Value;
  116.         return null;
  117.     },
  118.     
  119.     appendResource: function(aResourceOrID, aParentContainerOrID) {
  120.         var resource = this.getResource(aResourceOrID);
  121.         if (aParentContainerOrID) {
  122.             var parentContainer = (typeof(parentContainer)=="string") ?
  123.                 this.getContainer(aParentContainerOrID) : aParentContainerOrID;
  124.             parentContainer.AppendElement(resource);
  125.         }
  126.         return resource;
  127.     },
  128.     
  129.     removeResource: function(aResourceOrID, aParentContainerOrID) {
  130.         var resource = (typeof(aResourceOrID)=="string") ?
  131.                             this.getResource(aResourceOrID) : aResourceOrID;
  132.         
  133.         // Φª¬πé│πâ│πâåπâèπüïπéëπâ¬πé╜πâ╝πé╣πéÆσëèΘÖñ
  134.         if (aParentContainerOrID) {
  135.             var parentContainer = (typeof(aParentContainerOrID)=="string") ?
  136.                 this.getContainer(aParentContainerOrID) : aParentContainerOrID;
  137.             parentContainer.RemoveElement(resource, true);
  138.         }
  139.         
  140.         // πâ¬πé╜πâ╝πé╣πéÆσëèΘÖñ
  141.         var names = this._ds.ArcLabelsOut(resource);
  142.         var name, value;
  143.         while (names.hasMoreElements()) {
  144.             name = names.getNext().QueryInterface(Ci.nsIRDFResource);
  145.             value = this._ds.GetTarget(resource, name, true);
  146.             this._ds.Unassert(resource, name, value);
  147.         }
  148.         
  149.     },
  150.     
  151.     makeSeqContainer: function(aResourceID) {
  152.         var containerRes = this.getResource(aResourceID);
  153.         return this.rdfContainerUtils.MakeSeq(this._ds, containerRes);
  154.     },
  155.     
  156.     getContainer: function(aResourceID) {
  157.         try {
  158.             var containerRes = this.getResource(aResourceID);
  159.             var rdfContainer = Cc["@mozilla.org/rdf/container;1"]
  160.                         .createInstance(Ci.nsIRDFContainer);
  161.             rdfContainer.Init(this._ds, containerRes);
  162.             return rdfContainer;
  163.         } catch(ex) {}
  164.         return this.makeSeqContainer(aResourceID);
  165.     },
  166.     
  167.     clearContainer: function(aResourceID) {
  168.         var container = this.getContainer(aResourceID);
  169.         while (container.GetCount()) {
  170.             container.RemoveElementAt(1, true);
  171.         }
  172.     },
  173.     
  174.     search: function(aSearchString){
  175.         this.clearContainer(this.RDF_ITEM_SEARCH);
  176.         var searchContainer = this.getContainer(this.RDF_ITEM_SEARCH);
  177.         
  178.         var list = aSearchString.toUpperCase().replace(/ +/g, " ").replace(/ $/, "").split(" ");
  179.         
  180.         function checkKey(aKey, aList) {
  181.             var length = aList.length;
  182.             var rv = true;
  183.             for (var i=0; i<length; i++) {
  184.                 rv = rv && ( ( aKey.toUpperCase().indexOf(aList[i]) > -1 ) ? true : false );
  185.                 if (!rv) break;
  186.             }
  187.             return rv;
  188.         }
  189.         
  190.         var resources = this._ds.GetAllResources();
  191.         
  192.         var timer = Components.classes["@mozilla.org/timer;1"]
  193.                     .createInstance(Components.interfaces.nsITimer);
  194.         var self = this;
  195.         function timerCallback() {}
  196.         timerCallback.prototype = {
  197.             observe: function(aTimer, aTopic, aData) {
  198.                 var i = 0;
  199.                 while (resources.hasMoreElements() && i<100) {
  200.                     var resource = resources.getNext().QueryInterface(Ci.nsIRDFResource);
  201.                     var key = self.getLiteralProperty(resource, self.NS_CACHEVIEWER + "key");
  202.                     if (key && checkKey(key, list))
  203.                         searchContainer.AppendElement(resource);
  204.                     i++;
  205.                 }
  206.                 
  207.                 if (resources.hasMoreElements())
  208.                     timer.init(new timerCallback(), 0, timer.TYPE_ONE_SHOT);
  209.             }
  210.         }
  211.         timer.init(new timerCallback(), 0, timer.TYPE_ONE_SHOT);
  212.     }
  213. };
  214.